import { defineConfig,loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'node:path'
import cesium from 'vite-plugin-cesium'
import { viteMockServe } from 'vite-plugin-mock'
//引入svg需要用到插件
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import tsconfigPaths from 'vite-tsconfig-paths'

// https://vite.dev/config/
export default defineConfig(({command,mode})=>{
   //获取各种环境下的对应的变量
   let env = loadEnv(mode, process.cwd())
  return {
    base: './',
    build: {
      sourcemap: false,
      emptyOutDir: true
    },
    plugins: [
      vue({
        template: {
          compilerOptions: {
            isCustomElement: (tag) => ['md-linedivider'].includes(tag),
          }
        }
      }),
      cesium(),
      tsconfigPaths(),
      createSvgIconsPlugin({
        // Specify the icon folder to be cached
        iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],
        // Specify symbolId format
        symbolId: 'icon-[dir]-[name]',
      }),
      viteMockServe({
        enable: command === 'serve',//保证开发阶段可以使用mock接口
      }),
    ],
    //scss全局变量配置
    // css: {
    //   preprocessorOptions: {
    //     scss: {
    //       additionalData: `@import '@/styles/variable.scss';`
    //     }
    //   }
    // },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: '@use "@/styles/variable.scss" as *;',
        },
      },
    },
    resolve: {
        alias: {
            '@': path.resolve(__dirname, './src') // 相对路径别名配置，使用 @ 代替 src
        }
    },
    server: {
      proxy: {
        [env.VITE_APP_BASE_API]: {
          //获取数据服务器地址的设置
          target: env.VITE_SERVE,
          //需要代理跨域
          changeOrigin: true,
          //路径重写
          rewrite: (path) => path.replace(/^\/api/, ''),
          //确保请求头被传递
          headers: {
            Connection: 'keep-alive',
            'Cache-Control': 'no-cache',
          },
        },
        // 阿里云OSS代理配置
        '/oss-proxy': {
          //https://sna-application.oss-cn-beijing.aliyuncs.com
          target: 'https://sna-application.oss-cn-beijing.aliyuncs.com',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/oss-proxy/, ''),
          headers: {
            Connection: 'keep-alive',
            'Cache-Control': 'no-cache',
          },
        },
      },
    },
  }
})
